//热门智力题 - 打水问题  
//基本思想：用小桶容量的倍数对大桶的容量进行取余。  
//指导方针：不断用小桶装水倒入大桶，大桶满了立即清空，  
//每次判断下二个桶中水的容量是否等于指定容量。  
#include <iostream>
#include <vector>
using namespace std;
const string OPERATOR_NAME[7] = {  
    "装满A桶","装满B桶",  
    "将A桶清空","将B桶清空",  
    "A桶中水倒入B桶","B桶中水倒入A桶",  
    "成功"  
};  
int main()  
{  
    cout<<"热门智力题 - 打水问题"<<endl;  
    cout<<"  --by yuhanle( https://blog.yuhanle.com )--\n"<<endl;  
  
    int    a_volume, b_volume, goal_volume;  
    vector<string> record;       //记录操作步数  
    int    ai;  
    int    i, a_water, b_water;  
  
    cout<<"请输入A桶容量，B桶容量，目标容量：";  
    cin>>a_volume>>b_volume>>goal_volume;  
    a_water = b_water = 0; //A桶，B桶中有多少升水  
    char szTemp[30];  
    while (true)  
    {  
        if (a_water == 0)//A桶没水,就装满水  
        {  
            a_water = a_volume;  
            sprintf(szTemp, "         A:%d  B:%d", a_water, b_water);   
            record.push_back(OPERATOR_NAME[0] + szTemp);//fill A  
        }  
        else  
        {  
            //如果A桶的水比(B桶容量-B桶的水)要多  
            if (a_water > b_volume - b_water)  
            {  
                //A桶的水==A桶的水+B桶的水-B桶容量  
                a_water = a_water + b_water- b_volume;  
                b_water = b_volume;      //B桶的水装满了  
                sprintf(szTemp, "  A:%d  B:%d", a_water, b_water);   
                record.push_back(OPERATOR_NAME[4] + szTemp);//A->B     
                if (a_water == goal_volume)  
                    break;  
                b_water = 0;            //将B桶清空  
                sprintf(szTemp, "       A:%d  B:%d", a_water, b_water);   
                record.push_back(OPERATOR_NAME[3] + szTemp);  
            }  
            else  
            {  
                //B桶的水==A桶的水+B桶的水  
                b_water += a_water;   
                a_water = 0;  
                sprintf(szTemp, "  A:%d  B:%d", a_water, b_water);  
                record.push_back(OPERATOR_NAME[4] + szTemp);//A->B  
                if (b_water == goal_volume)   
                    break;  
            }  
        }  
    }  
    record.push_back(OPERATOR_NAME[6]); //success  
    cout<<"\n---------------------------------------------------"<<endl;  
    cout<<"一个可行的倒水方案如下"<<endl;  
    vector::iterator pos;  
    for (pos = record.begin(); pos != record.end(); pos++)  
        cout<<*pos<<endl;  
    cout<<"---------------------------------------------------"<<endl;  
    return 0;  
}
